using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinDock;
using CPGVertragsverwaltung.DatabaseAccess;
using System.Diagnostics;
using CPGVertragsverwaltung.ErrorHandling;
using System.IO;
using Infragistics.Win.UltraWinTabControl;


namespace CPGVertragsverwaltung.Viewer
{
    public partial class ObjectDetailViewer : UserControl
    {
        private int mCurrentContractID = 0;

        #region Events, Delegate
        public event DataChangedDelegate DataChanged;
        #endregion

        #region Properties
        #endregion

        #region OnEvent
        private void ObjectDetailViewer_Load(object sender, EventArgs e)
        {

        }

        private void OnDataChanged()
        {
            if (this.DataChanged != null)
            {
                this.DataChanged();
            }
        }

        private void OnAttachmentChanged()
        {
            loadAttachments(Convert.ToString(mCurrentContractID));
        }
        #endregion

        public ObjectDetailViewer()
        {
            InitializeComponent();
        }




        /// <summary>
        /// load Contract with the given ID
        /// </summary>
        /// <param name="ID">ID</param>
        public void loadContract(int ID)
        {
            mCurrentContractID = ID;

            //create new ContractLayout
            Control parentControl = contractLayout1.Parent;
            contractLayout1.Dispose();

            contractLayout1 = new ContractLayout();
            parentControl.Controls.Add(contractLayout1);
            contractLayout1.Dock = DockStyle.Fill;
            contractLayout1.BringToFront();
            contractLayout1.DataChanged += new DataChangedDelegate(OnDataChanged);
            contractLayout1.AttachmentChanged += new DataChangedDelegate(OnAttachmentChanged);

            contractLayout1.loadContract(ID);

            //Load SecuritySettings
            securitySettings1.loadContract(mCurrentContractID);
            securitySettings1.Enabled = GlobalVariables.UserIsAdmin;

            //Load Attachments
            this.loadAttachments(Convert.ToString(mCurrentContractID));
        }

        public void saveContract()
        {
            if (mCurrentContractID <= 0)
                return;
            
            DBUpdateBuilder myUpdateBuilder = new DBUpdateBuilder("vertrag", "vertrag_id",contractLayout1.ID);
            myUpdateBuilder.addStringPair(new StringPair("vertrag_beschreibung", contractLayout1.Titel));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_notizen", contractLayout1.Notize));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_isaktiv", Convert.ToString(contractLayout1.Aktiv)));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_vertragspartei", contractLayout1.Vertragspartei));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_werk", contractLayout1.Vertragswerk));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_art", contractLayout1.Vertragsart));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_gegenstand", contractLayout1.Vertragsgegenstand));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_vertragsdatum", contractLayout1.Vertragsdatum));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_vertragsbeginn", contractLayout1.Beginn));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_vertragsende", contractLayout1.Ende));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_fortsetzung", contractLayout1.Fortsetzung));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_kuendigungsfrist", contractLayout1.Kuendigungsfrist));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_kuendigungsdatum", contractLayout1.Kuendigungsdatum));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_entschaedigung", contractLayout1.Entschaedigung));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_kontrolldatum", contractLayout1.Kontrolldatum));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_zahlungsdatum", contractLayout1.Zahlungsdatum));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_zahlungsmethode", contractLayout1.Zahlungsmethode));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_zahlweise", contractLayout1.Zahlweise));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_zustaendiger", contractLayout1.Zustaendiger));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_ablage", contractLayout1.Ablage));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_konto", contractLayout1.Konto));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_kostenstelle", contractLayout1.Kostenstelle));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_ertrag", contractLayout1.Ertraege));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_kosten", contractLayout1.Kosten));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_mwst", contractLayout1.MWSt));
            myUpdateBuilder.addStringPair(new StringPair("vertrag_mwstcode", contractLayout1.MWStCode));

            Debug.WriteLine(myUpdateBuilder.getUpdateString());

            //Update Data To Database
            int Lines = 0;
            String SQLStatement = myUpdateBuilder.getUpdateString();
            Lines = DBProvider.doSQLQuery(SQLStatement);
            if (Lines < 1)
            {
                MessageHandling.showError("Der Vertrag mit ID " + contractLayout1.ID + " konnte nicht aktualisiert werden!");
            }
            else
            {
                StatusPanels.Action.Text = Lines + " Datenzeile wurde aktualisiert";
            }

            //reload Contract
            this.loadContract(mCurrentContractID);
            



        }

        /// <summary>
        /// Create Tabs with Filenames
        /// </summary>
        /// <param name="VertragID"></param>
        private void loadAttachments(String VertragID)
        {
            String FolderPath = GlobalVariables.AttachmentDirectory + "Vertrag" + VertragID;
            if (!Directory.Exists(FolderPath))
                return;

            //Clear all Tabs
            tbctrAttachments.Tabs.Clear();

            //Add Tab with Key,Filename, ToolTipText
            DirectoryInfo DirInfo=new DirectoryInfo(FolderPath);
            foreach (FileInfo myFile in DirInfo.GetFiles())
            {
                UltraTab myTab = new UltraTab();
                myTab.Key = myFile.FullName;
                myTab.ToolTipText = myFile.FullName;
                if (myFile.Name.Length > 15)
                {
                    myTab.Text = myFile.Name.Substring(0, 15);
                    myTab.Text += "...";
                }
                else
                {
                    myTab.Text = myFile.Name;
                }
                
                   
                tbctrAttachments.Tabs.Add(myTab);
            }

        }


        /// <summary>
        /// create TabPageControl when TabControl is selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbctrAttachments_SelectedTabChanged(object sender, SelectedTabChangedEventArgs e)
        {
            if (e.Tab == null)
                return;

            tbctrAttachments.Height = 400;

             UltraTab selectedTab = e.Tab;
             selectedTab.TabPage.Controls.Clear();

             FileInfo FInfo = new FileInfo(selectedTab.Key);
             switch (FInfo.Extension.ToLower())
             {
                 case ".pdf":
                     AxAcroPDFLib.AxAcroPDF AcrobatAX = new AxAcroPDFLib.AxAcroPDF();
                     selectedTab.TabPage.Controls.Add(AcrobatAX);
                     AcrobatAX.Left = 0; AcrobatAX.Top = 0; AcrobatAX.Width = 300; AcrobatAX.Height = 200;
                     AcrobatAX.Dock = DockStyle.Fill;
                     AcrobatAX.LoadFile(selectedTab.Key);
                     AcrobatAX.Show();
                     break;
                 case ".doc":
                     /*
                     Panel myPanel = new Panel();
                     selectedTab.TabPage.Controls.Add(myPanel);
                     myPanel.Dock = DockStyle.Fill;
                     clsWord myWord = new clsWord(selectedTab.Key);
                     myWord.EmbedTo(myPanel);
                      */
                     break;
             }
        }
    }
}
